﻿using Estsh.Core.Controllers;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Wms.IServices;
using Microsoft.AspNetCore.Mvc;

namespace Estsh.Core.Web.Plugin.Wms.Areas.wms.Controllers.Split
{
    [Route("wms/[controller]")]
    [ApiController]
    public class OpenControl : WmsBaseController
    {
        private IOpenControlService openControlService;
        public OpenControl(IOpenControlService _openControl)
        {
            openControlService = _openControl;
        }

        /// <summary>
        /// 获取工作中心
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetPdlines")]
        public IActionResult GetPdlines(int loginid)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<Syspdline_Extend> obj = openControlService.GetPdlines(loginid);
                result.Success = true;
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 获取工作中心下所对应的工单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("GetWorkOrder")]
        public IActionResult GetWorkOrder(int id)
        {
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                List<MesWorkOrder> obj = openControlService.GetWorkOrder(id);
                result.Success = true; 
                result.Data = obj;
            }
            catch (Exception ex)
            {
                result.Msg=ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 获取工作中心对应点检项
        /// </summary>
        /// <param name="pdlineId"></param>
        /// <returns></returns>
        [HttpPost("GetSpotCheck")]
        public IActionResult GetSpotCheck(int pdlineId)
        {
            WmsResponseResult result=new WmsResponseResult();
            try
            {
                List<SysSpotCheck> obj = openControlService.GetSpotChecks(pdlineId);
                result.Success = true;
                result.Data = obj;
            }
            catch(Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 获取工单对应的零件和状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pdlineid"></param>
        /// <returns></returns>
        [HttpPost("GetPartNoStatus")]
        public IActionResult GetPartNoStatus(int id,int pdlineid)
        {
            WmsResponseResult resultresult = new WmsResponseResult();
            try
            {
                List<MesWorkOrder_Extend> orders=openControlService.GetPartNoStatus(id);
                if (orders.Count > 0)
                {
                    //如果数量大于0则工单有正在点检和点检未通过的
                    resultresult.Data = 1;
                    //int resu = openControlService.UpdateCheckStatus(id, DateTime.Now.ToString("yyyy-MM-dd"));
                    //resultresult.Data = true;
                    resultresult.Success = true;

                }
                else
                {
                    List<MesWorkOrder_Extend> extends=openControlService.GetCheckOk(id);
                    if (extends.Count > 0)
                    {
                        int resu = openControlService.UpdateCheckStatus(id, DateTime.Now.ToString("yyyy-MM-dd"));
                        resultresult.Data = "ok";
                        resultresult.Success = true;
                    }
                    else
                    {
                        List<SysOrderCheck> sysOrderChecks = openControlService.GetOrderChecks(id);
                        if (sysOrderChecks.Count > 0)
                        {
                            resultresult.Data = sysOrderChecks;
                            resultresult.Success = true;

                        }
                        else
                        {
                            //List<MesWorkOrder_Extend> workOrders = openControlService.GetOrders(id);
                            bool result = openControlService.AddOrderCheck(id, pdlineid);
                            List<SysOrderCheck> orderChecks = openControlService.GetOrderChecks(id);
                            resultresult.Data = orderChecks;
                            resultresult.Success = result;
                        }
                    }
                    
                }

            }
            catch (Exception ex)
            {
                resultresult.Msg = ex.Message;
            }
            return Json(resultresult);
        }

        /// <summary>
        /// 点击通过/不通过后更改对应的状态
        /// </summary>
        /// <param name="orderid"></param>
        /// <param name="checkid"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        [HttpPost("UpdateCheckStat")]
        public IActionResult UpdateCheckStat(int orderid,int checkid,int status,int pdlineid,int loginid)
        {
            WmsResponseResult result=new WmsResponseResult();
            try
            {
                int resu = openControlService.UpdateCheckStatus(orderid, checkid, status,pdlineid,loginid);
                result.Data = resu;
                result.Success = true;
            }
            catch(Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        ///  获取工单对应的点检记录
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        [HttpPost("GetOrderCheck")]
        public IActionResult GetOrderCheck(string orderid)
        {
            WmsResponseResult result= new WmsResponseResult();
            try
            {
                List<SysOrderCheck> orderChecks=openControlService.GetSysOrderChecks(orderid);
                result.Data = orderChecks;
                result.Success = true;
            }
            catch(Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 重新点检
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("UpdateAgain")]
        public IActionResult UpdateAgain(Newtonsoft.Json.Linq.JObject jobj)
        {
            int id =int.Parse( jobj["id"].ToString().Trim());
            int pdlineid =int.Parse( jobj["pdlineid"].ToString().Trim());
            string loginid = jobj["loginid"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                openControlService.UpdateAgain(id,pdlineid,loginid);
                result.Data=null;
                result.Success = true;
            }
            catch(Exception ex) 
            { 
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 点检完成提交
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost("UpdateWorkOrder")]
        public IActionResult UpdateWorkOrder(int orderid, int pdlineid)
        {
            WmsResponseResult result= new WmsResponseResult();
            try
            {
                int resu= openControlService.UpdateWorkOrder(orderid, pdlineid);
                result.Data=resu;
                result.Success = true;
            }
            catch(Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 获取历史操作记录
        /// </summary>
        /// <param name="pdlineid"></param>
        /// <returns></returns>
        [HttpPost("GetCheckFinish")]
        public IActionResult GetCheckFinish(int pdlineid)
        {
            WmsResponseResult result=new WmsResponseResult();
            try
            {
                List<MesWorkOrder_Extend> extends=openControlService.GetCheckFinish(pdlineid);
                result.Data=extends;
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 获取点检未通过原因
        /// </summary>
        /// <param name="checkid"></param>
        /// <returns></returns>
        [HttpPost("GetCheckReason")]
        public IActionResult GetCheckReason(int checkid)
        {
            WmsResponseResult result=new WmsResponseResult();
            try
            {
                List<SysSpotCheckReason> checkReasons = openControlService.GetCheckReasons(checkid);
                
                result.Data=checkReasons;
                result.Success = true;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 添加点检不通过记录
        /// </summary>
        /// <param name="reasonid"></param>
        /// <param name="checkid"></param>
        /// <returns></returns>
        [HttpPost("AddReasonLog")]
        public IActionResult AddReasonLog(Newtonsoft.Json.Linq.JObject jobj)
        {
            int reasonid = int.Parse(jobj["reasonid"].ToString().Trim());
            int checkid = int.Parse(jobj["checkid"].ToString().Trim());
            string loginid = jobj["loginid"].ToString().Trim();
            WmsResponseResult result = new WmsResponseResult();
            try
            {
                int resu = openControlService.AddReasonLog(reasonid,checkid,loginid);
                result.Success = true;
                result.Data = resu;
            }
            catch (Exception ex)
            {
                   result.Msg = ex.Message;
            }
            return Json(result);
        }

        /// <summary>
        /// 工作中心最大点检数量
        /// </summary>
        /// <param name="pdlineid"></param>
        /// <returns></returns>
        [HttpPost("GetPdlineMaxCount")]
        public IActionResult GetPdlineMaxCount(int pdlineid, int orderid)
        {
            WmsResponseResult result= new WmsResponseResult();
            try
            {
                int resu = openControlService.GetPdlineMaxCount(pdlineid,orderid);
                result.Success = true;
                result.Data = resu;
            }
            catch (Exception ex)
            {
                result.Msg = ex.Message;
            }
            return Json(result);
        }

    }
}
